package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Xzdy;
import com.corpcredit.webapp.action.BasePage;
import com.corpcredit.service.GenericManager;

public class XzdyForm extends BasePage implements Serializable {
    private GenericManager<Xzdy, Long> xzdyManager;
    private Xzdy xzdy = new Xzdy();
    private Long id;
    private Jbqk jbqk;
	
	public void setJbqk(Jbqk jbqk)
	{
		this.jbqk=jbqk;
	}

    public void setXzdyManager(GenericManager<Xzdy, Long> xzdyManager) {
        this.xzdyManager = xzdyManager;
    }

    public Xzdy getXzdy() {
        return xzdy;
    }

    public void setXzdy(Xzdy xzdy) {
        this.xzdy = xzdy;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        xzdyManager.remove(xzdy.getId());
        addMessage("xzdy.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            xzdy = xzdyManager.get(id);
        } else {
            xzdy = new Xzdy();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (xzdy.getId() == null || xzdy.getId() == 0);
        
        
        xzdy.setJbqk(jbqk);
        xzdy.setUsername(getRequest().getRemoteUser());
        xzdy.setOptdate(new Date());
        xzdy.setOperation(isNew?Constants.OPERATION_ADD:Constants.OPERATION_UPDATE);
        
      

        xzdyManager.save(xzdy);

        String key = (isNew) ? "xzdy.added" : "xzdy.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
          	
     	   this.id=this.xzdy.getId();
              return edit();

        }
    }
} 